leetcode 94 Binary Tree Inorder Traversal

recursive method

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

non-recursive method

My non-recursive version is attached below. The basic idea is making sure that the stack peek node is the first node with no left son in the current traversal context.

Here is an explanation of code:

Add node having left son to the stack until encountering a node with no left son, after that, the stack peek is the node with no left son, and should be added to the result list and poped from stack. Then assign the node's right son to variable node, so that we can do the 'add node with left son to stack until encountering a node with no left son' thing again in the next loop.

Due to my implementation, there's one case to be dealt with, which is when a node has no right son. In this case, just assign null to the current node, and check if node is null in the inner while.

/**
 * 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> ret = new ArrayList<>();
        if (root == null) return ret;
        Stack<TreeNode> s = new Stack<TreeNode>();
        TreeNode node = root;
        s.add(root);
        while (!s.isEmpty()) {
            while (node != null && node.left != null) {
                node = node.left;
                s.add(node);
            }
            node = s.pop();
            ret.add(node.val);
            if (node.right != null) {
                s.add(node.right);
            }
            node = node.right;
            
        }
        return ret;
    }
}

I also checked official solution, and found it's code more elegant. And here's is my non-recursive version 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> ret = new ArrayList<>();
        Stack<TreeNode> s = new Stack<TreeNode>();
        TreeNode node = root;
        while (node != null || !s.isEmpty()) {
            while (node != null) {
                s.add(node);
                node = node.left;
            }
            node = s.pop();
            ret.add(node.val);
            node = node.right;
            
        }
        return ret;
    }

}
posted on 2019-04-10 10:39  王 帅  阅读(95)  评论(0编辑  收藏  举报