94. Binary Tree Inorder Traversal 做题报告

题目链接:

94. Binary Tree Inorder Traversal

题目大意:

二叉树的中序遍历

做题报告:

(1)该题涉及的算法,数据结构以及相关知识点

递归

(2)自己的解答思路+代码+分析时间和空间复杂度

递归思路

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        //给定二叉树,求中序遍历
        List<Integer> ans = new ArrayList<Integer>();
        help(root,ans);
        return ans;
    }
    public void help(TreeNode root,List<Integer> ans) {
        if(root==null) return;
        if(root.left != null)  help(root.left,ans);
        ans.add(root.val);
        if(root.right != null)  help(root.right,ans);
    }
}

时间复杂度:O(n)  递归函数 T(n)=2⋅T(n/2)+1
空间复杂度:最坏情况下需要空间O(n),平均情况为O(log?n)

(3)经典解答思路+代码+分析时间和空间复杂度

方法一:递归

方法二:基于栈的遍历

注明:可看官方题解,有动画PPT

public class Solution {
    public List < Integer > inorderTraversal(TreeNode root) {
        List < Integer > res = new ArrayList < > ();
        Stack < TreeNode > stack = new Stack < > ();
        TreeNode curr = root;
        while (curr != null || !stack.isEmpty()) {
            while (curr != null) {
                stack.push(curr);
                curr = curr.left;
            }
            curr = stack.pop();
            res.add(curr.val);
            curr = curr.right;
        }
        return res;
    }
}
作者:LeetCode
链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/solution/er-cha-shu-de-zhong-xu-bian-li-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

方法三:莫里斯遍历

方法四:颜色标记法

其核心思想如下:
    使用颜色标记节点的状态,新节点为白色,已访问的节点为灰色。
    如果遇到的节点为白色,则将其标记为灰色,然后将其右子节点、自身、左子节点依次入栈。
    如果遇到的节点为灰色,则将节点的值输出。


class Solution {

class ColorNode {

TreeNode node;

String color;

public ColorNode(TreeNode node,String color){

this.node = node;

this.color = color;

}

}

public List<Integer> inorderTraversal(TreeNode root) {

if(root == null) return new ArrayList<Integer>();

List<Integer> res = new ArrayList<>();

Stack<ColorNode> stack = new Stack<>();

stack.push(new ColorNode(root,"white"));

while(!stack.empty()){

ColorNode cn = stack.pop();

if(cn.color.equals("white")){

if(cn.node.right != null) stack.push(new ColorNode(cn.node.right,"white"));

stack.push(new ColorNode(cn.node,"gray"));//gray标记该节点写入了答案列表

if(cn.node.left != null) stack.push(new ColorNode(cn.node.left,"white"));

}else{

res.add(cn.node.val);

}

}

return res;

}

}

(4)比较自己想的和参考答案的区别

          用栈与递归这种思路,递归效率不高,栈迭代方法虽然提高了效率,但其嵌套循环却非常烧脑,不易理解,容易造成“一看就懂,一写就废”的窘况。而且对于不同的遍历顺序(前序、中序、后序),循环结构差异很大,更增加了记忆负担。比较推荐第四种解法。

原文地址:https://www.cnblogs.com/Aiahtwo/p/12229973.html

时间: 2024-11-06 11:14:43

94. Binary Tree Inorder Traversal 做题报告的相关文章

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? 在做99. Recover Binary Search Tree 时,要求不用O(n)的

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(二叉树中序遍历) 解题思路和方法

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

94.Binary Tree Inorder Traversal(非递归中序遍历)

Given a binary tree, return the inorder traversal of itsnodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution istrivial, could you do it iteratively? confused what "{1,#,2,3}" means? > rea

94. Binary Tree Inorder Traversal(Tree, stack)

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? 法I: recursion /** * Definition

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 OJ 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? Subscribe to see which companies asked this

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

[leedcode 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? > read