/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Soluti

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
                  if(pre.size()==0||vin.size()==0)
                             return NULL;
                         TreeNode*  p=new TreeNode(pre[0]);
                            int i;
              for(i=0;i<vin.size();i++)
                  if(pre[0]==vin[i])
                      break;
               vector<int> left_pre,left_vin,right_pre,right_vin;
                 for(int j=0;j<i;j++)
                 {left_pre.push_back(pre[j+1]);
                         left_vin.push_back(vin[j]);}

                for(int j=i+1;j<pre.size();j++)
                {right_pre.push_back(pre[j]);
                         right_vin.push_back(vin[j]);}
                 p->left=reConstructBinaryTree(left_pre,left_vin);
                 p->right=reConstructBinaryTree(right_pre,right_vin);
               return p;
    }
};

  

时间: 2024-08-03 07:03:50

/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Soluti的相关文章

LeetCode (15) Flatten Binary Tree to Linked List

题目描述 Given a binary tree, flatten it to a linked list in-place. For example, Given The flattened tree should look like: 本题也是考察二叉树和指针操作的题目.题目要求将一棵二叉树拉平为一个链表 .链表通过树节点的右子树相连,且展开的顺序为原来树的前序遍历. 实现思路: 若节点n存在左子树left,则将左子树连接为n的右子树 若n存在右子树(n->right存在),则将n->ri

Leetcode: Binary Tree Postorder Traversal(二叉树后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 递归解法(C++版本): /** * Definition for binary

[LeetCode]Balanced Binary Tree

Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 这道题以前整理过,这里就不细写了,直接贴上代码: /** * Defi

[C++]LeetCode: 95 Binary Tree Preorder Traversal (先序遍历)

题目: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? Answer 1: 递归法 思路:如果当前节点不为空,先把当前节点的值放入数组.从

Maximum Depth of Binary Tree 树的最大深度

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Hide Tags Tree Depth-first Search /** * Definition for binary tree * struct TreeNode { * i

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

leetcode 刷题之路 64 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. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

[Leetcode][Tree][Binary Tree Postorder Traversal]

二叉树的后续遍历 1.递归版本 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void dfsPostorderTraversal(TreeNode *now, vec

二叉树最大路径和-Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. For exampl