LeetCode 94 Binary Tree Inorder Traversal (中序遍历二叉树)

Given a binary tree, return the inorder traversal of its nodes‘ values.

For example:

Given binary tree [1,null,2,3],

   1
         2
    /
   3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

题目链接:https://leetcode.com/problems/binary-tree-inorder-traversal/

题目分析:中序遍历二叉树,递归的方法简单明了,不多说了

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    void DFS(TreeNode root, List<Integer> ans) {
        if(root == null) {
            return;
        }
        DFS(root.left, ans);
        ans.add(root.val);
        DFS(root.right, ans);
    }

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        DFS(root, ans);
        return ans;
    }
}

下面是非递归的做法,用一个栈模拟递归的过程,中序遍历的顺序是 左->中->右,先一直走到最左端,取值后pop,这个过程其实完成了左->中的遍历,每次pop后再往右走,这样就完成了左->中->右的遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        while(root != null || !stack.empty()) {
            while(root != null) {
                stack.push(root);
                root = root.left;
            }
            if(!stack.empty()) {
                root = stack.peek();
                stack.pop();
                ans.add(root.val);
                root = root.right;
            }
        }
        return ans;
    }
}
时间: 2024-10-21 04:49:27

LeetCode 94 Binary Tree Inorder Traversal (中序遍历二叉树)的相关文章

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

Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree [1,null,2,3], 1 2 / 3 return [1,3,2]. Note: Recursive(递归) solution is trivial, could you do it iteratively(迭代)? 思路: 解法一:用递归方法很简单, (1)如果root为空,则返回NU

LeetCode 144 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 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题目链接:https://leetcode.com/problems/binary-tre

leetCode 94.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? > rea

leetcode 94 Binary Tree Inorder Traversal ----- java

Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree [1,null,2,3], 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 求二叉树的中序遍历,要求不是用递归. 先用递归做一下,很简单. /** * Defini

Leetcode 94 Binary Tree Inorder Traversal 二叉树

二叉树的中序遍历,即左子树,根, 右子树 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 void dfs(vect

Leetcode #94 Binary Tree Inorder Traversal

题目链接:https://leetcode.com/problems/binary-tree-inorder-traversal/ (非递归实现)二叉树的中序遍历. 1 class Solution 2 { 3 public: 4 vector<int> inorderTraversal(TreeNode *root) 5 { 6 vector<int> output; 7 if(root == NULL) 8 { 9 return output; 10 } 11 12 stack

LeetCode 94 Binary Tree Inorder Traversal(二叉树的中序遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其中序遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [1, 3, 2] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 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: Recursi

leetcode 94.Binary Tree Inorder Traversal 二叉树的中序遍历

递归算法C++代码: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> in

[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? confused what "{1,#,2,3}" means? > read