leetcode 145+590 二叉树后序遍历(iterative)(附589,144,94)

题目描述

145. Binary Tree Postorder Traversal

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

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3

Output: [3,2,1]
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
    LinkedList<TreeNode> stack = new LinkedList<>();
    LinkedList<Integer> output = new LinkedList<>();
    if (root == null) {
      return output;
    }

    stack.add(root);
    while (!stack.isEmpty()) {
      TreeNode node = stack.pollLast();
      output.addFirst(node.val);
      if (node.left != null) {
        stack.add(node.left);
      }
      if (node.right != null) {
        stack.add(node.right);
      }
    }
    return output;
  }
}

590. N-ary Tree Postorder Traversal

Given an n-ary tree, return the postorder traversal of its nodes' values.

For example, given a 3-ary tree:

Return its postorder traversal as: [5,6,3,2,4,1].

class Solution {
    public List<Integer> postorder(Node root) {
        LinkedList<Node> stack= new LinkedList<>();
        LinkedList<Integer> output= new LinkedList<>();
        if(root==null)
            return output;
        stack.add(root);
        while(!stack.isEmpty()){
            Node tmp= stack.pollLast();
            output.addFirst(tmp.val);
                for(Node n: tmp.children){
                    if(n!=null)
                         stack.add(n);
                }
                   
        }
        return output;
    }
    
}

145与590两道题只有树节点的构造稍微不同,实现原理都是相同的。要求不用递归,迭代做、

下图是solution解释中的图,非原创

通过上图可发现:

BFS:从上至下,从左至右

DFS:左右根有三种排列方式

144. Binary Tree Preorder Traversal

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

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3

Output: [1,2,3]
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        LinkedList<TreeNode> stack= new LinkedList<>();
        LinkedList<Integer> output= new LinkedList<>();
        if(root==null)
            return output;
        stack.add(root);
        while(!stack.isEmpty()){
            TreeNode tmp= stack.pollLast();
            output.addLast(tmp.val);
            if(tmp.right!=null){
                stack.addLast(tmp.right);
            }
            if(tmp.left!=null){
                stack.addLast(tmp.left);
            }
        }
        return output;
    }
}

589. N-ary Tree Preorder Traversal

Given an n-ary tree, return the preorder traversal of its nodes' values.

For example, given a 3-ary tree:

Return its preorder traversal as: [1,3,5,6,2,4].

class Solution {
    public List<Integer> preorder(Node root) {
        LinkedList<Node> stack= new LinkedList<>();
        LinkedList<Integer> output= new LinkedList<>();
        if(root==null)
            return output;
        stack.add(root);
        while(!stack.isEmpty()){
            Node tmp= stack.pollLast();
            output.addLast(tmp.val);
            int index= tmp.children.size()-1;
            for(;index>=0;index--){
                Node chil= tmp.children.get(index);
                if(chil!=null)  stack.addLast(chil);
            }
        }
        return output;
    }
}

94. Binary Tree Inorder Traversal

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

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3

Output: [1,3,2]
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        LinkedList<TreeNode> stack= new LinkedList<>();
        LinkedList<Integer> output= new LinkedList<>();
        if(root==null)
            return output;
        stack.add(root);
        TreeNode cur= root.left;
        while(cur!=null || !stack.isEmpty()){
            while(cur!=null){
                stack.addLast(cur);
                cur= cur.left;
            }
            cur= stack.pollLast();
            output.addLast(cur.val);
            cur= cur.right;
        }
        return output;
    }
}

猜你喜欢

转载自blog.csdn.net/better_girl/article/details/85037675