94. Binary Tree Inorder Traversal

Description

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

For example:
Given binary tree [1,null,2,3],

tree

return [1,3,2].

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

Solution

DFS, time O(n), space O(1)

/**
 * 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> inorder = new LinkedList<>();
        inorderTraversalRecur(root, inorder);
        return inorder;
    }
    
    public void inorderTraversalRecur(TreeNode root, List<Integer> inorder) {
        if (root == null) {
            return;
        }
        
        inorderTraversalRecur(root.left, inorder);
        inorder.add(root.val);
        inorderTraversalRecur(root.right, inorder);
    }
}

Iterative using Stack, time O(n), space O(n)

I use pushAllLeft() to push all the left children of one Node into the stack, so that my idea looks clear:

  1. We push all the left children of root into the stack until there’s no more nodes.
  2. Then we pop from the stack which we’d call cur.
  3. Add cur to result list
  4. Recursively call pushAllLeft() on cur's right child.
/**
 * 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> inorder = new LinkedList<>();
        Stack<TreeNode> stack = new Stack<>();
        pushAllLeft(root, stack);
        
        while (!stack.empty()) {
            TreeNode leftMost = stack.pop();
            inorder.add(leftMost.val);
            pushAllLeft(leftMost.right, stack);
        }
        
        return inorder;
    }
    
    public void pushAllLeft(TreeNode root, Stack<TreeNode> stack) {
        while (root != null) {
            stack.push(root);
            root = root.left;
        }
    }
}

Common solution, time O(n), space O(h)

/**
 * 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> list = new LinkedList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode p = root;
        
        while (!stack.empty() || p != null) {
            if (p != null) {
                stack.push(p);
                p = p.left;
            } else {
                p = stack.pop();
                list.add(p.val);    // add after left child visited
                p = p.right;
            }
        }
        
        return list;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容