LeetCode: Binary Tree Inorder Traversal 解题报告

Binary Tree Inorder Traversal

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.

SOL:

包括递归与非递归方法:

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public List<Integer> inorderTraversal1(TreeNode root) {
12         List<Integer> ret = new ArrayList<Integer>();
13         rec(root, ret);
14         return ret;
15     }
16
17     public void rec(TreeNode root, List<Integer> ret) {
18         if (root == null) {
19             return;
20         }
21
22         rec(root.left, ret);
23         ret.add(root.val);
24         rec(root.right, ret);
25     }
26
27     public List<Integer> inorderTraversal(TreeNode root) {
28         List<Integer> ret = new ArrayList<Integer>();
29         if (root == null) {
30             return ret;
31         }
32
33         Stack<TreeNode> s = new Stack<TreeNode>();
34         TreeNode cur = root;
35
36         while (true) {
37             while (cur != null) {
38                 s.push(cur);
39                 cur = cur.left;
40             }
41
42             if (s.isEmpty()) {
43                 break;
44             }
45
46             cur = s.pop();
47             ret.add(cur.val);
48
49             cur = cur.right;
50         }
51
52         return ret;
53     }
54 }

时间: 2024-10-27 08:36:17

LeetCode: Binary Tree Inorder Traversal 解题报告的相关文章

LeetCode: Binary Tree Postorder Traversal 解题报告

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3},   1    \     2    /   3return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively

LeetCode: Binary Tree Preorder Traversal 解题报告

Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3},   1    \     2    /   3return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively?

[leetcode]Binary Tree Inorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ 题意:二叉树的中序遍历. 解题思路:这道题用递归解不难,所以应该考察的是非递归求解二叉树的中序遍历.我们使用一个栈来解决问题.比如一颗二叉树为{1,2,3,4,5,6,7},第一层为{1},第二层为{2,3},第三层为{4,5,6,7}.那么我们依次存储左子树的根节点,那么入栈顺序为:1,2,4.由于4的左子树为空,所以开始出栈.4出栈,检查4的右子树为空,继续

LeetCode: Binary Tree Inorder Traversal [094]

[题目] 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? >

LeetCode——Binary Tree Inorder Traversal

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? 原题链接:https://oj.leetcode.com/problems/binary-t

【LeetCode】145. Binary Tree Postorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51494797 Subject 出处:https://leetcode.com/problems/binary-tree-postorder-traversal/ Hard 级别 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree

LeetCode Binary Tree Inorder Traversal

LeetCode解题之Binary Tree Inorder Traversal 原题 不用递归来实现树的中序遍历. 注意点: 无 例子: 输入: {1,#,2,3} 1 2 / 3 输出: [1,3,2] 解题思路 通过栈来实现,从根节点开始,不断寻找左节点,并把这些节点依次压入栈内,只有在该节点没有左节点或者它的左子树都已经遍历完成后,它才会从栈内弹出,这时候访问该节点,并它的右节点当做新的根节点一样不断遍历. AC源码 # Definition for a binary tree node

Leetcode: Binary Tree Inorder Traversal(二叉树中序遍历)

题目: 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? 递归解法(C++): /** * Definition for binary tre

LeetCode 144. Binary Tree Preorder Traversal 解题报告

144. Binary Tree Preorder Traversal My Submissions Question Total Accepted: 108336 Total Submissions: 278322 Difficulty: Medium Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3