【LeetCode】根据前序和中序遍历建树 && 根据中序遍历和后续遍历建树

133 篇文章 0 订阅
121 篇文章 2 订阅
1、Construct Binary Tree from Preorder and Inorder Traversal 
Total Accepted: 7041 Total Submissions: 27696 My Submissions
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
2、Construct Binary Tree from Inorder and Postorder Traversal 
Total Accepted: 6863 Total Submissions: 26686 My Submissions
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
根据前序遍历和中序遍历建树以及根据中序遍历和后序遍历建树。
基本思想是递归,需要注意的是边界的处理。
关于前序遍历和后序遍历建树,树是不唯一的,在 Jobdu 题目1044:Pre-Post已有详细说明,请参考。

1 Java AC

/**
 * 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[] preorder, int[] inorder) {
		if (preorder == null || preorder.length == 0) {
			return null;
		}
		if (inorder == null || inorder.length == 0) {
			return null;
		}
		int len1 = preorder.length - 1;
		int len2 = inorder.length - 1;
		if (len1 != len2) {
			return null;
		}
		return dfs(preorder, inorder, 0, len2, 0);
	}

	private TreeNode dfs(int[] preorder, int[] inorder, int low,
			int high, int index) {
		if (low > high) {
			return null;
		}
		TreeNode node = new TreeNode(preorder[index]);
		if (low == high) {
			return node;
		}
		int pos = searchInsert(inorder, preorder[index], low, high);
		node.left = dfs(preorder, inorder, low, pos - 1, index + 1);
		node.right = dfs(preorder, inorder, pos + 1, high, index + 1 + (pos - low));
		return node;
	}

	private int searchInsert(int[] array, int t, int low, int high) {
		int pos = 0;
		for (int i = low; i <= high; i++) {
			if (array[i] == t) {
				pos = i;
				break;
			}
		}
		return pos;
	}
}

2 Java AC

/**
 * 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) {
        if (inorder == null || inorder.length == 0) {
			return null;
		}
		if (postorder == null || postorder.length == 0) {
			return null;
		}
		int len1 = inorder.length - 1;
		int len2 = postorder.length - 1;
		if (len1 != len2) {
			return null;
		}
		return dfs(inorder, postorder, 0, len1, len2);
	}

	private TreeNode dfs(int[] inorder, int[] postorder, int low,
			int high, int index) {
		if (low > high) {
			return null;
		}
		TreeNode node = new TreeNode(postorder[index]);
		if (low == high) {
			return node;
		}
		int pos = searchInsert(inorder, postorder[index], low, high);
		node.left = dfs(inorder, postorder, low, pos - 1, index - 1 - (high - pos));
		node.right = dfs(inorder, postorder, pos + 1, high, index - 1);
		return node;
	}

	private int searchInsert(int[] array, int t, int low, int high) {
		int pos = 0;
		for (int i = low; i <= high; i++) {
			if (array[i] == t) {
				pos = i;
				break;
			}
		}
		return pos;
	}
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值