如何根据“前序遍历,中序遍历”,“中序遍历,后序遍历”构建按二叉树

如何根据前序遍历和中序遍历,或者中序遍历和后序遍历创建二叉树?大致思路如下文章;

http://t.csdn.cn/wBUiy


 分析:

 

 

代码如下: 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int preIndex;
    public TreeNode buildTreeChild(int[] preorder, int[] inorder, int inBegan, int inEnd){
        //如果ib > ie说明递归终止
        if(inBegan > inEnd){
            return null;
        }
        //创建根节点
        TreeNode root = new TreeNode(preorder[preIndex]);
        //在中序遍历中找到对应的根节点下标
        int InorderIndex = fundInorderIndex(inorder, preorder[preIndex], inBegan, inEnd);
        //前序遍历中的下标继续往后遍历
        preIndex++;
        //通过递归继续创建左子树和右子树
        root.left = buildTreeChild(preorder, inorder, inBegan, InorderIndex - 1);
        root.right = buildTreeChild(preorder, inorder, InorderIndex + 1, inEnd);
        //最后返回根结点即可
        return root;
    }
    public int fundInorderIndex(int[] inorder, int val, int inBegan, int inEnd){
        for(int i = inBegan; i <= inEnd; i++){
            if(inorder[i] == val){
                return i;
            }
        }
        return -1;
    }
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTreeChild(preorder, inorder, 0, inorder.length - 1);
    }
}

分析: 

         和前序与中序遍历的思路一样,但是注意的是,后序遍历中是从后向前找根结点,因此,当你写出后序遍历的顺序的时候,会发现需要先创建右子树,再创建左子树

代码如下:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int preIndex;
    public TreeNode buildTreeChild(int[] postorder, int[] inorder, int inBegan, int inEnd){
        //如果ib > ie说明递归终止
        if(inBegan > inEnd){
            return null;
        }
        //创建根节点
        TreeNode root = new TreeNode(postorder[preIndex]);
        //在中序遍历中找到对应的根节点下标
        int InorderIndex = fundInorderIndex(inorder, postorder[preIndex], inBegan, inEnd);
        //前序遍历中的下标继续往后遍历
        preIndex--;
        //这里要注意,先构建左树再构建右树!
        root.right = buildTreeChild(postorder, inorder, InorderIndex + 1, inEnd);
        root.left = buildTreeChild(postorder, inorder, inBegan, InorderIndex - 1);

        //最后返回根结点即可
        return root;
    }
    public int fundInorderIndex(int[] inorder, int val, int inBegan, int inEnd){
        for(int i = inBegan; i <= inEnd; i++){
            if(inorder[i] == val){
                return i;
            }
        }
        return -1;
    }
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        preIndex = postorder.length - 1;
        return buildTreeChild(postorder, inorder, 0, inorder.length - 1);
    }
}

 

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈亦康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值