Leetcode-94-二叉树的中序遍历

Leetcode-94、二叉树的中序遍历

一、题目

二、题解

递归

Java题解

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List inorderTraversal(TreeNode root) {
        List res = new ArrayList<>();
        _inorder(root, res);
        return res;
    }
    private void _inorder(TreeNode root, List res) {
        if (root == null) {
            return;
        }
        _inorder(root.left, res);
        res.add(root.val);
        _inorder(root.right, res);
    }
}
结果
image.png
迭代
Java题解1
class Solution {
    public List inorderTraversal(TreeNode root) {
        List res = new ArrayList<>();
        TreeNode p = root;
        Stack s = new Stack<>();
        while (p != null || !s.empty()) {
            // 不断往左子树方向走,每走一次就将当前节点保存到栈中
            if (p != null) {    // p不为空,一直向左走
                s.push(p);
                p = p.left;
            } else {    // p为空,说明走到头了,弹出栈顶元素,保存,再往右走
            // 当前节点为空,说明左边走到头了,从栈中弹出节点并保存, 然后转向右边节点,继续上面整个过程
                TreeNode tmp = s.pop();
                res.add(tmp.val);
                p = tmp.right;
            }
        }
        return res;
    }
}
Java题解2
// 左-根-右
class Solution {
    public List inorderTraversal(TreeNode root) {
        List res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        Stack stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            while (root != null) {
                stack.push(root.left);
                root = root.left;
            }
            root = stack.pop();
            if (root != null) {
                res.add(root.val);
                stack.push(root.right);
                root = root.right;
            }
        }
        return res;
    }
}
Java题解3

标答

class Solution {
    public List inorderTraversal(TreeNode root) {
        List res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        Stack stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            if (cur != null) {
                res.add(cur.val);
                cur = cur.right;
            }
        }
        return res;
    }
}
Python题解

标答

def inorderTraversal(self, root):
    res, stack = [], []
    while True:
        while root:
            stack.append(root)
            root = root.left
        if not stack:
            return res
        node = stack.pop()
        res.append(node.val)
        root = node.right

参考

1、优秀题解
[https://leetcode-cn.com/problems/binary-tree-inorder-traversal/solution/dong-hua-yan-shi-94-er-cha-shu-de-zhong-xu-bian-li/](https://leetcode-cn.com/problems/binary-tree-inorder-2
2、
https://leetcode.com/problems/binary-tree-inorder-traversal/discuss/31404/Concise-JAVA-solution-based-on-Stack
3、
https://leetcode.com/problems/binary-tree-inorder-traversal/discuss/31381/Python-recursive-and-iterative-solutions.

你可能感兴趣的:(Leetcode-94-二叉树的中序遍历)