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;
    		}
        }
    }
    

     

  • 相关阅读:
    Java遍历Map键、值。获取Map大小的方法
    Oracle CASE WHEN 用法介绍
    JS动态改变select选择变更option的index值
    js对select动态添加和删除OPTION
    在js中使用createElement创建HTML对象和元素
    清空select标签中option选项的3种不同方式
    json-lib包笔记
    异常:javax.el.PropertyNotFoundException: Property 'id' not found on ..........
    golang struct的使用
    golang多维数组的切片
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3553030.html
Copyright © 2011-2022 走看看