[leetcode]Binary Tree InorderTraversal

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.

算法思路:

1. 递归实现

 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     List<Integer> res = new ArrayList<Integer>();
12     public List<Integer> inorderTraversal(TreeNode root) {
13         if(root == null) return res;
14         if(root.left != null) inorderTraversal(root.left);
15         res.add(root.val);
16         if(root.right != null) inorderTraversal(root.right);
17         return res;
18     }
19 }

思路2:

非递归版本:

借用栈,将当前节点的所有左节点(左节点的左节点....)压栈,然后依次弹栈处理当前节点并处理右子树

代码如下:

 1 public class Solution {
 2     public List<Integer> inorderTraversal(TreeNode root) {
 3         List<Integer> res = new ArrayList<Integer>();
 4         if(root == null) return res;
 5         Stack<TreeNode> stack = new Stack<TreeNode>();
 6         TreeNode current = root;
 7         while(true){
 8             while(current != null){
 9                 stack.push(current);
10                 current = current.left;
11             }
12             if(stack.isEmpty()) break;
13             current = stack.pop();
14             res.add(current.val);
15             current = current.right;
16         }
17         return res;
18     }
19 }
时间: 2024-10-23 10:16:38

[leetcode]Binary Tree InorderTraversal的相关文章

LeetCode &quot;Binary Tree *-order Traversal&#39; by Iteration

Binary Tree *-order traversal by recursion is trivial. But their iteration version deserves a look: Pre-Order class Solution { vector<int> ret; public: vector<int> preorderTraversal(TreeNode *p) { if (!p) return ret; stack<TreeNode *> st

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

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: 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 @ 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

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] Binary Tree Preorder/Inorder/Postorder Traversal

前中后遍历 递归版 1 /* Recursive solution */ 2 class Solution { 3 public: 4 vector<int> preorderTraversal(TreeNode *root) { 5 6 vector<int> result; 7 preorderTraversal(root, result); 8 9 return result; 10 } 11 12 void preorderTraversal(TreeNode *root,

LeetCode—— Binary Tree Traversal

二叉树遍历的三道题 Binary Tree Preorder Traversal Binary Tree Inorder Traversal Binary Tree Postorder Traversal LeetCode 上二叉树的节点定义如下: // 树的节点 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {

[leetcode]Binary Tree Zigzag Level Order Traversal

Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#