二叉树的非递归遍历

非递归遍历,都是用栈实现,本质上都是用栈的进出来模仿递归行为,任何递归解法都可以转换成栈来解决。

1、先序遍历

144. 二叉树的前序遍历 - 力扣(LeetCode)

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if (root != null) {
            stack.push(root);
            while(!stack.isEmpty()) {
                root = stack.pop();
                ans.add(root.val);
                if (root.right != null) {
                    stack.push(root.right);
                }
                if (root.left != null) {
                    stack.push(root.left);
                }
            }
        }
        return ans;
    }
}

中序遍历

94. 二叉树的中序遍历 - 力扣(LeetCode)

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

后续遍历解法一:

145. 二叉树的后序遍历 - 力扣(LeetCode)

用两个栈实现,这种方法是建立在前序遍历代码的基础上(前序遍历是中->左->右,稍微改动代码,将这个顺序改成中->右->左,然后建立一个新栈,将所有节点push到这个新栈中,这样从这个新栈中弹出来的顺序就是左->右->中,即为后序遍历!)

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        Stack<TreeNode> stack_ans = new Stack<>();
        List<Integer> ans = new ArrayList<>();
        if (root != null) {
            stack.push(root);
            while(!stack.isEmpty()) {
                root = stack.pop();
                stack_ans.push(root);
                if (root.left != null) {
                    stack.push(root.left);
                }
                if (root.right != null) {
                    stack.push(root.right);
                }
            }
        }
        while (!stack_ans.isEmpty()) {
            ans.add(stack_ans.pop().val);
        }
        return ans;
    }
}

后续遍历解法2

145. 二叉树的后序遍历 - 力扣(LeetCode)

用一个栈实现,对比后序遍历解法1,只用到一个栈,空间复杂度较好。

class Solution {
    public List<Integer> postorderTraversal(TreeNode h) {
        List<Integer> ans = new ArrayList<>();
        if (h != null) {
            Stack<TreeNode> stack = new Stack<>();
            stack.push(h);
            // 如果始终没有打印过节点,h就一直是头节点
			// 一旦打印过节点,h就变成打印节点
			// 之后h的含义 : 上一次打印的节点
            while (!stack.isEmpty()) {
                TreeNode cur = stack.peek();
                if (cur.left != null && cur.left != h && cur.right!=h)   {
                    stack.push(cur.left);
                }  else if(cur.right != null && cur.right != h) {
                    stack.push(cur.right);
                } else {
                    ans.add(cur.val);
                    h = stack.pop();
                }
            }
        }
        return ans;
    }
}

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值