【LeetCode】Binary Tree Inorder Traversal 二叉树中序遍历递归以及非递归算法
2014-05-07 18:21
447 查看
Binary Tree Inorder Traversal
Total Accepted: 16494 Total Submissions: 47494
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree
return
Note: Recursive solution is trivial, could you do it iteratively?
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
【解题思路】
1、递归
Java AC
栈存储节点,当左孩子不为空,一直入栈。否则出栈,同时将栈顶元素存入list中,针对当前节点右孩子做同样操作。
Java AC
Total Accepted: 16494 Total Submissions: 47494
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree
{1,#,2,3},
1 \ 2 / 3
return
[1,3,2].
Note: Recursive solution is trivial, could you do it iteratively?
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
【解题思路】
1、递归
Java AC
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { private ArrayList<Integer> list; public ArrayList<Integer> inorderTraversal(TreeNode root) { list = new ArrayList<Integer>(); inOrder(root); return list; } private void inOrder(TreeNode root){ if(root == null){ return; } inOrder(root.left); list.add(root.val); inOrder(root.right); } }2、非递归
栈存储节点,当左孩子不为空,一直入栈。否则出栈,同时将栈顶元素存入list中,针对当前节点右孩子做同样操作。
Java AC
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { private ArrayList<Integer> list; public ArrayList<Integer> inorderTraversal(TreeNode root) { list = new ArrayList<Integer>(); inOrder(root); return list; } private void inOrder(TreeNode root){ Stack<TreeNode> stack = new Stack<TreeNode>(); while(root != null || !stack.isEmpty()){ while(root != null){ stack.push(root); root = root.left; } if(!stack.isEmpty()){ root = stack.pop(); list.add(root.val); root = root.right; } } } }
相关文章推荐
- 【LeetCode】Binary Tree Preorder Traversal 二叉树前序遍历递归以及非递归算法
- Leetcode 94:Binary Tree Inorder Traversal (二叉树中序遍历,非递归)
- Leetcode Binary Tree Inorder Traversal 二叉树中序遍历
- leetcode_105题——Construct Binary Tree from Preorder and Inorder Traversal(树,递归)
- LeetCode 94 Binary Tree Inorder Traversal(二叉树中序遍历)
- LeetCode-Construct Binary Tree from Preorder and Inorder Traversal-前序中序恢复二叉树-递归
- 【LeetCode】Binary Tree Postorder Traversal 二叉树后序遍历递归以及非递归算法
- leetcode_94题——Binary Tree Inorder Traversal (二叉树,递归,队列queue,栈stack,set)
- [leetCode]Binary Tree Inorder Traversal 递归 && 栈解法
- Binary Tree Inorder Traversal 二叉树中序遍历,使用堆栈,非递归
- Leetcode: Binary Tree Inorder Traversal(二叉树中序遍历)
- [LeetCode] Binary Tree Inorder Traversal [递归版]
- [Leetcode]Binary Tree Inorder Traversal@python(附前、中、后三种遍历的递归和非递归方法!)
- LeetCode-Construct Binary Tree from Inorder and Postorder Traversal-中序后序重构二叉树-递归
- 【LeetCode-面试算法经典-Java实现】【094-Binary Tree Inorder Traversal(二叉树中序遍历)】
- LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium
- Binary Tree Inorder Traversal [leetcode] 非递归的三种解法
- 【LeetCode】Binary Tree Inorder Traversal ---二叉树中序遍历
- LeetCode(94)Binary Tree Inorder Traversal
- C++详解Leetcode:105. Construct Binary Tree from Preorder and Inorder Traversal