二叉树--后序遍历

  • 后序遍历:左右根,从叶子结点开始

    //递归
    public static void postOrder(TreeNode root){
        if(root != null){
        	postOrder(root.lchild);
    		postOrder(root.rchild);
        	System.out.println(root.data + " ");
        }
    }
    
    //后序遍历---非递归
    public List<TreeNode> postOrder(TreeNode root){
    	while(root!=null || !stack.isEmpty()){
    		while(root!=null){
    			list.add(root);
    			static.push(root);
    			root = root.right;
    		}
    		TreeNode node = stack.pop();
    		root = node.left;
    	}
    	Conlections.reverse(list);
    	return list;
    }
    
上一篇:kubernetes-概念


下一篇:p34 二叉树的后续遍历 (leetcode 145)