LeetCode105 Construct Binary Tree from Preorder and Inorder Traversal
2017-04-27 22:00
477 查看
详细见:leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal
Java Solution:
github
package leetcode;
import tools.TreeNode辅助.TreeNode;
public class P105_ConstructBinaryTreeFromPreorderAndInorderTraversal {
public static void main(String[] args) {
}
/*
* AC
* 19 ms
*/
static class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder == null || inorder == null) {
return null;
}
if (preorder.length != inorder.length) {
return null;
}
return build(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
}
private TreeNode build(int[] preorder, int i, int j, int[] inorder, int k, int l) {
if (i > j || k > l) {
return null;
}
TreeNode root = null;
int root_val = preorder[i];
int in_index = k;
for (; in_index <= l; in_index ++) {
if (inorder[in_index] == root_val) {
break;
}
}
root = new TreeNode(root_val);
root.left = build(preorder, i + 1, i + in_index - k, inorder, k, in_index - 1);
root.right = build(preorder, i + in_index - k + 1, j, inorder, in_index + 1, l);
return root;
}
}
}
C Solution:
github
/*
url: leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal
AC 13ms 14.29%
*/
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
typedef struct TreeNode stn;
typedef struct TreeNode * ptn;
ptn tn_init(int val) {
ptn t = (ptn) malloc(sizeof(stn));
t->val = val;
t->left = NULL;
t->right = NULL;
return t;
}
ptn search(int* p, int pi, int pj, int* i, int ii, int ij) {
int lt = 0, rt = 0;
ptn n = NULL;
if (pi > pj) return NULL;
while(i[ii+lt] != p[pi]) lt ++;
rt = pj-pi-lt;
n = tn_init(p[pi]);
n->left = search(p, pi+1, pi+lt, i, ii, ii+lt-1);
n->right = search(p, pj-rt+1, pj, i, ij-rt+1, ij);
return n;
}
ptn buildTree(int* p, int pn, int* i, int in) {
return search(p, 0, pn-1, i, 0, in-1);
}
Python Solution:
github
#coding=utf-8
'''
url: leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年4月27日
@details: Solution: 495ms 11.75%
'''
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def search(self, p, pi, pj, n, ni, nj):
if pi > pj: return None
r = TreeNode(p[pi])
lt = 0
while n[ni+lt] != p[pi]:
lt += 1
rt = pj-pi-lt
r.left = self.search(p, pi+1, pi+lt, n, ni, ni+lt-1)
r.right = self.search(p, pj-rt+1, pj, n, nj-rt+1, nj)
return r
def buildTree(self, p, n):
"""
:type p: List[int]
:type i: List[int]
:rtype: TreeNode
"""
pn = 0 if p == None else len(p)
nn = 0 if n == None else len(n)
return self.search(p, 0, pn-1, n, 0, nn-1)
Java Solution:
github
package leetcode;
import tools.TreeNode辅助.TreeNode;
public class P105_ConstructBinaryTreeFromPreorderAndInorderTraversal {
public static void main(String[] args) {
}
/*
* AC
* 19 ms
*/
static class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder == null || inorder == null) {
return null;
}
if (preorder.length != inorder.length) {
return null;
}
return build(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
}
private TreeNode build(int[] preorder, int i, int j, int[] inorder, int k, int l) {
if (i > j || k > l) {
return null;
}
TreeNode root = null;
int root_val = preorder[i];
int in_index = k;
for (; in_index <= l; in_index ++) {
if (inorder[in_index] == root_val) {
break;
}
}
root = new TreeNode(root_val);
root.left = build(preorder, i + 1, i + in_index - k, inorder, k, in_index - 1);
root.right = build(preorder, i + in_index - k + 1, j, inorder, in_index + 1, l);
return root;
}
}
}
C Solution:
github
/*
url: leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal
AC 13ms 14.29%
*/
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
typedef struct TreeNode stn;
typedef struct TreeNode * ptn;
ptn tn_init(int val) {
ptn t = (ptn) malloc(sizeof(stn));
t->val = val;
t->left = NULL;
t->right = NULL;
return t;
}
ptn search(int* p, int pi, int pj, int* i, int ii, int ij) {
int lt = 0, rt = 0;
ptn n = NULL;
if (pi > pj) return NULL;
while(i[ii+lt] != p[pi]) lt ++;
rt = pj-pi-lt;
n = tn_init(p[pi]);
n->left = search(p, pi+1, pi+lt, i, ii, ii+lt-1);
n->right = search(p, pj-rt+1, pj, i, ij-rt+1, ij);
return n;
}
ptn buildTree(int* p, int pn, int* i, int in) {
return search(p, 0, pn-1, i, 0, in-1);
}
Python Solution:
github
#coding=utf-8
'''
url: leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年4月27日
@details: Solution: 495ms 11.75%
'''
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def search(self, p, pi, pj, n, ni, nj):
if pi > pj: return None
r = TreeNode(p[pi])
lt = 0
while n[ni+lt] != p[pi]:
lt += 1
rt = pj-pi-lt
r.left = self.search(p, pi+1, pi+lt, n, ni, ni+lt-1)
r.right = self.search(p, pj-rt+1, pj, n, nj-rt+1, nj)
return r
def buildTree(self, p, n):
"""
:type p: List[int]
:type i: List[int]
:rtype: TreeNode
"""
pn = 0 if p == None else len(p)
nn = 0 if n == None else len(n)
return self.search(p, 0, pn-1, n, 0, nn-1)
相关文章推荐
- leetcode[105]Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode 105] Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 105: Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal
- leetcode_105_Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode---(105)Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode 105, Medium] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal [105]
- [Leetcode 78] 105 Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]105 Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode 105] Construct Binary Tree from Preorder and Inorder Traversal
- [JAVA]LeetCode105 Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105/106 Construct Binary Tree from Preorder/Postorder and Inorder Traversal
- 【LeetCode-面试算法经典-Java实现】【105-Construct Binary Tree from Preorder and Inorder Traversal(构造二叉树)】
- LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode#105]Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium
- [leetcode-105]Construct Binary Tree from Preorder and Inorder Traversal(java)