Binary Tree Postorder Traversal

来源:互联网 发布:电脑无损音乐播放软件 编辑:程序博客网 时间:2024/05/17 23:01
  1. 题目 Binary Tree Postorder Traversal
    Given a binary tree, return the postorder traversal of its nodes’ values.
    给出一棵二叉树,返回其节点值的后序遍历。
  2. 算法
    递归:先遍历左节点,遍历右节点,访问跟节点
    public ArrayList<Integer> postorderTraversal(TreeNode root) {        // write your code here        ArrayList<Integer> res = new ArrayList<Integer>();        if (root == null) {            return res;        }        helper(root, res);        return res;    }    public void helper(TreeNode root, ArrayList<Integer> res)    {        if (root != null) {            helper(root.left, res);   //遍历左节点            helper(root.right, res);  //遍历右节点            res.add(root.val);        //访问根节点        }    }

非递归方法比较复杂,我们用cur表示当前节点,用pre表示前一个遍历的节点,步骤如下

  1. 当pre的左节点或右节点为cur,表示在向下遍历,如果cur有左节点,则左节点进栈,如果有右节点,右节点进栈,如果cur为叶节点,则直接访问并出栈
  2. 当cur的左节点为pre时,说明在回溯,根据后续遍历准则,如果cur有右节点,则右节点进栈,如果没有右节点,则访问并出栈
  3. 当cur的右节点为pre时,说明左右节点都访问了,访问cur并出栈
    public ArrayList<Integer> postorderTraversal(TreeNode root) {        // write your code here        ArrayList<Integer> res = new ArrayList<Integer>();        if (root == null) {            return res;        }        TreeNode pre = null;        LinkedList<TreeNode> stack = new LinkedList<TreeNode>();        stack.push(root);        while(!stack.isEmpty()) {     // 当栈不空时循环            TreeNode cur = stack.peek();  // 当前元素为栈顶元素            if (pre == null || pre.left == cur || pre.right == cur) {  //第一个元素进栈时pre为空                if (cur.left != null) {                    stack.push(cur.left);  // 右节点进栈                } else if (cur.right != null) {                    stack.push(cur.right);  //左节点进栈                } else {                    res.add(cur.val);   //为叶节点,访问并弹栈                    stack.pop();                }            } else if (cur.left == pre && cur.right != null) {                 stack.push(cur.right);  // 右节点进栈            } else {                 // pre为cur右节点,或右节点为空                res.add(cur.val);                  stack.pop();            }            pre = cur;         //  最后交换        }        return res;

其实上面代码还有一中写法

    public ArrayList<Integer> postorderTraversal(TreeNode root) {        // write your code here        ArrayList<Integer> res = new ArrayList<Integer>();        if (root == null) {            return res;        }        TreeNode pre = null;        LinkedList<TreeNode> stack = new LinkedList<TreeNode>();        while (root != null || !stack.isEmpty()) {            if (root != null) {                stack.push(root);                root = root.left;            } else {                  TreeNode peekNode = stack.peek();                if (peekNode.right != null && pre != peekNode.right) {// 当右节点不为空或没访问过                    root = peekNode.right;                } else {                    res.add(peekNode.val);                    stack.pop();                    pre = peekNode;                }            }        }        return res;    }
0 0
原创粉丝点击