【数据结构_二叉树+dfs】
来源:互联网 发布:杂志排版用什么软件 编辑:程序博客网 时间:2024/06/08 02:02
Flatten Binary Tree to Linked List
Total Accepted: 17814 Total Submissions: 64054My SubmissionsGiven a binary tree, flatten it to a linked list in-place.
For example,
Given
1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6
/** * 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 flatten(TreeNode *root) { dfs(root); } TreeNode *dfs(TreeNode *root){ if(root==NULL) return NULL; TreeNode *left=root->left,*leftTail=dfs(root->left), *right=root->right,*rightTail=dfs(root->right); if(left) { root->right=left,root->left=NULL; if(right){ leftTail->right=right; return rightTail; }else{ return leftTail; } }else{//left child is null if(right) return rightTail; else return root;//left and right are null } }};
Validate Binary Search Tree
Total Accepted: 16968 Total Submissions: 66123My SubmissionsGiven a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
class Solution {public: bool isValidBST(TreeNode *root) { if(root==NULL) return true; Pr tmp;return dfs(root,tmp); } typedef pair<int,int> Pr; bool dfs(TreeNode *root,Pr &pr){ assert(root!=NULL); int _min=root->val,_max=root->val; if(root->left) { Pr p1; if(!dfs(root->left,p1)) return false; if(p1.second>=root->val) return false; //@@error: pair p1; p1->second should be p1.second.//@error:p1.second>root->val,should be >= _min=min(_min,p1.first); } if(root->right) { Pr p2; if(!dfs(root->right,p2)) return false; if(p2.first<=root->val) return false; _max=max(_max,p2.second); } pr=Pr(_min,_max); return true; }};
或者
为dfs(Node * root,int l,int r)//检验root是否在[l,r]范围内,不是则不合格
class Solution {public:#define MIN 0x80000000#define MAX 0x7FFFFFFF bool isValidBST(TreeNode *root) { return dfs(root,MIN,MAX); } bool dfs(TreeNode *root,int l,int r){ if(root==NULL) return true; int val=root->val; if(val<l||val>r) return false; return dfs(root->left,l,val-1)&&dfs(root->right,val+1,r); }};
0 0
- 【数据结构_二叉树+dfs】
- 数据结构_二叉树
- 数据结构_二叉查找树
- 数据结构_二叉树基础
- 数据结构_二叉索引树_模板
- 数据结构_树形结构_二叉树
- 数据结构:树与二叉树_二叉树_定义
- 数据结构_查找_平衡二叉树_插入
- [数据结构复习]树_二叉树
- 【数据结构_树_Tree_AVL_1077】平衡二叉树
- 数据结构的C实现_二叉树
- 数据结构课程设计题目四_二叉树
- 数据结构_二叉树的遍历
- 数据结构与算法_二叉查找树
- [数据结构]实验五_顺序二叉树
- 数据结构_二叉树_遍历算法应用
- 数据结构:树与二叉树_二叉树_二叉树的基本操作
- [数据结构复习]树_二叉搜索树&AVL树
- Android学习笔记1 及今日遇到的问题
- Zend Studio 10正式版注册破解
- 软件测试工程师笔试题目和答案1
- 亲爱的程序猿们怎么找工作
- POJ 3435 Sudoku Checker
- 【数据结构_二叉树+dfs】
- 基本输入输出
- 触摸屏驱动分析之——ts_test.c
- 做互联网赚钱的几种模式总结。。。
- POJ 1562
- 学习jquery01
- drawable的用法
- apache kafka系列之broker内部架构
- 在VS2010中ActiveX控件注册方法,使用regsvr32命令