zoukankan      html  css  js  c++  java
  • leetcode--Binary Tree Postorder Traversal

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

    For example:
    Given binary tree {1,#,2,3},

       1
        
         2
        /
       3
    

    return [3,2,1].

    Note: Recursive solution is trivial, could you do it iteratively?

    /**
     * Definition for binary tree
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ArrayList<Integer> postorderTraversal(TreeNode root) {
            ArrayList<Integer> result = new ArrayList<Integer>();
            if(root != null){
                Stack<TreeNode> sta = new Stack<TreeNode>();
                sta.push(root);
                HashSet<TreeNode> hset = new HashSet<TreeNode>();
                hset.add(root);
                while(!sta.empty()){
                    TreeNode aNode = sta.pop();
                    if(aNode.left != null && hset.add(aNode.left)){
                        sta.push(aNode);
                        sta.push(aNode.left);        
                    }
                    else if(aNode.right != null && hset.add(aNode.right)){
                        sta.push(aNode);
                        sta.push(aNode.right);                
                    }
                    else
                        result.add(aNode.val);
                } 
            }
            return result;
        }
    }
    

    Another solution:

    public class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
           List<Integer> postOrderTreeValue = new ArrayList<Integer>();
    		Stack<TreeNode> TreeNodeStack = new Stack<TreeNode>();
    		TreeNode haveDoneRightNode = null; 
    		
    		//initialize the stack with TreeNodes
    		if(root != null)
    			putTreeNodeInStack(TreeNodeStack, root);
    		while(!TreeNodeStack.isEmpty()){
    			TreeNode currentNode = TreeNodeStack.pop();
    			if(currentNode.right == null || currentNode.right == haveDoneRightNode){
    				postOrderTreeValue.add(currentNode.val);
    				if(!TreeNodeStack.isEmpty() && currentNode == TreeNodeStack.peek().right)
    					haveDoneRightNode = currentNode;
    			}
    			else{
    				TreeNodeStack.push(currentNode);
    				putTreeNodeInStack(TreeNodeStack, currentNode.right);
    			}
    		}
    		return postOrderTreeValue;
    	}
    	
    	private void putTreeNodeInStack(Stack<TreeNode> TreeNodeStack, TreeNode root){
    		while(root !=null){
    			TreeNodeStack.push(root);
    			if(root.left != null)
    				root = root.left;
    			else if(root.right != null)
    				root = root.right;
    			else
    				root = null;
    		}
        }
    }
    

     

  • 相关阅读:
    为WebApi项目增加用户注册功能
    仿MVC ModelBinder功能
    C++你可能不知道地方
    跟我一起学XNA让移动物体撞起来(附源码)
    系统计数器与硬件分析
    好用的 VS扩展
    SearchEasy SearchEngine Framework(搜易SDK) 开源
    Debian Linux下的Python学习——入门
    一步一步实现网站的多语言版本
    asp.net的各种调试方法
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3553030.html
Copyright © 2011-2022 走看看