Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序重建二叉树@LeetCode

本文介绍了一种通过中序遍历和后序遍历构建二叉树的方法。文章提供了一个具体的例子,展示了如何根据两个遍历序列找到二叉树的根节点,并递归地构建整棵树。

同上一题,这一题是由inorder和postorder来确定树,对于postorder,root要从尾部开始找

另外,在从一个order到另外一个order时,要用相对距离来计算!即从第一个order算出dist是多少,然后应用这个dist到第二个order上


package Level4;

import Utility.TreeNode;

/**
 * Construct Binary Tree from Inorder and Postorder Traversal
 * 
 *  Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.
 *
 */
public class S141 {

	public static void main(String[] args) {
		int[] inorder = {1,2,3};
		int[] postorder = {3,2,1};
		
		TreeNode root = buildTree(inorder, postorder);
		root.print();
	}

	public static TreeNode buildTree(int[] inorder, int[] postorder) {
		if(inorder.length == 0){
			return null;
		}
        return rec(inorder, postorder, 0, inorder.length-1, 0, postorder.length-1);
    }
	
	public static TreeNode rec(int[] inorder, int[] postorder, int inStart, int inEnd, int postStart, int postEnd){
		if(postEnd < 0 || postEnd>=postorder.length){
			return null;
		}
		TreeNode root = new TreeNode(postorder[postEnd]);
		int rootIndex;			// rootIndex in inorder[]
		for(rootIndex=0; rootIndex<inorder.length; rootIndex++) {
			if(inorder[rootIndex] == postorder[postEnd]){
				break;
			}
		}
		
		int leftSubTreeLen = rootIndex - inStart;		// inorder中,inStart到rootIndex的距离
		if(rootIndex > inStart){		// 锁定范围,否则会Memory out of limit!
			root.left = rec(inorder, postorder, inStart, rootIndex-1, postStart, postStart+leftSubTreeLen-1);
		}
		if(rootIndex < inEnd){
			root.right = rec(inorder, postorder, rootIndex+1, inEnd, postStart+leftSubTreeLen, postEnd-1);
		}
		
		return root;
	}
}



/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        return rec(inorder, postorder, 0, inorder.length-1, 0, postorder.length-1);
    }
    
    public TreeNode rec(int[] inorder, int[] postorder, int inorderLeft, int inorderRight, int postorderLeft, int postorderRight) {
        if(inorderLeft > inorderRight || postorderLeft > postorderRight) {
            return null;
        }
        int root = postorder[postorderRight];
        TreeNode rootNode = new TreeNode(root);
        int inorderRootPos = inorderLeft;
        for(inorderRootPos=inorderLeft; inorderRootPos<=inorderRight; inorderRootPos++) {
            if(inorder[inorderRootPos] == root) {
                break;
            }
        }
        
        int dist = inorderRootPos - inorderLeft;
        rootNode.left = rec(inorder, postorder, inorderLeft, inorderRootPos-1, postorderLeft, postorderLeft+dist-1);
        rootNode.right = rec(inorder, postorder, inorderRootPos+1, inorderRight, postorderLeft+dist, postorderRight-1);
        
        return rootNode;
    }
}



二叉树是一种树形结构,其中每个节点最多有两个子节点。建立二叉树的方法有很多种,其中一种常用的方法是通过输入带空格的前列建立二叉链表。具体步骤如下: 1. 读取输入的带空格的前列,将其存储在一个数组中。 2. 定义一个栈,将根节点入栈。 3. 从数组中取出下一个元素,如果该元素不为空,则将其作为当前节点的左子节点,并将该节点入栈。 4. 如果该元素为空,则从栈中弹出一个节点,将其作为当前节点,并从数组中取出下一个元素,将其作为当前节点的右子节点,并将该节点入栈。 5. 重复步骤34,直到数组中的所有元素都被处理完毕。 建立二叉树后,可以通过递归或非递归的方式进行先、中后序遍历。其中,先遍历的顺是先遍历根节点,然后遍历左子树,最后遍历右子树;中遍历的顺是先遍历左子树,然后遍历根节点,最后遍历右子树;后序遍历的顺是先遍历左子树,然后遍历右子树,最后遍历根节点。 以下是PHP基于非递归算法实现先、中后序遍历二叉树的代码示例: ```php // 定义二叉树节点类 class TreeNode { public $val; public $left; public $right; public function __construct($val) { $this->val = $val; $this->left = null; $this->right = null; } } // 建立二叉树 function buildTree($preorder) { $stack = array(); $root = new TreeNode(array_shift($preorder)); array_push($stack, $root); while (!empty($preorder)) { $node = array_shift($preorder); if ($node !== null) { $cur = new TreeNode($node); $stack[count($stack) - 1]->left = $cur; array_push($stack, $cur); } else { $stack[count($stack) - 1]->right = null; array_pop($stack); } } return $root; } // 非递归实现先遍历 function preorderTraversal($root) { $stack = array(); $res = array(); array_push($stack, $root); while (!empty($stack)) { $node = array_pop($stack); if ($node !== null) { array_push($res, $node->val); array_push($stack, $node->right); array_push($stack, $node->left); } } return $res; } // 非递归实现中遍历 function inorderTraversal($root) { $stack = array(); $res = array(); $cur = $root; while ($cur !== null || !empty($stack)) { while ($cur !== null) { array_push($stack, $cur); $cur = $cur->left; } $node = array_pop($stack); array_push($res, $node->val); $cur = $node->right; } return $res; } // 非递归实现后序遍历 function postorderTraversal($root) { $stack = array(); $res = array(); array_push($stack, $root); while (!empty($stack)) { $node = array_pop($stack); if ($node !== null) { array_push($res, $node->val); array_push($stack, $node->left); array_push($stack, $node->right); } } return array_reverse($res); } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值