美文网首页
中序遍历

中序遍历

作者: crazydane | 来源:发表于2017-06-06 01:13 被阅读0次
    public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> result = new ArrayList<Integer>();
            Deque<TreeNode> stack = new ArrayDeque<TreeNode>();
    
            TreeNode curr = root;
            while (curr != null || !stack.isEmpty()) {
                if (curr != null) {
                    stack.push(curr);
                    curr = curr.left;
                } else {
                    curr = stack.pop();
                    result.add(curr.val);
                    curr = curr.right;
                }
            }
    
            return result;
        }
    

    相关文章

      网友评论

          本文标题:中序遍历

          本文链接:https://www.haomeiwen.com/subject/agowfxtx.html