一、计算布尔二叉树的值
1、题目描述
leetcode链接
2、代码
class Solution
{
public :
bool evaluateTree ( TreeNode* root)
{
if ( root-> left == nullptr )
{
if ( root-> val == 0 )
{
return false ;
}
else
{
return true ;
}
}
bool Left = evaluateTree ( root-> left) ;
bool Right = evaluateTree ( root-> right) ;
if ( root-> val == 2 )
{
return Left | Right;
}
else
{
return Left & Right;
}
}
} ;
3、解析
二、求根节点到叶节点数字之和
1、题目描述
leetcode链接
2、代码
class Solution
{
public :
int sumNumbers ( TreeNode* root)
{
return dfs ( root, 0 ) ;
}
int dfs ( TreeNode* root, int persum)
{
persum = persum * 10 + root-> val;
if ( root-> left == nullptr && root-> right == nullptr )
{
return persum;
}
int ret = 0 ;
if ( root-> left)
{
ret += dfs ( root-> left, persum) ;
}
if ( root-> right)
{
ret += dfs ( root-> right, persum) ;
}
return ret;
}
} ;
3、解析
三、二叉树剪枝
1、题目描述
leetcode链接
2、代码
class Solution
{
public :
TreeNode* pruneTree ( TreeNode* root)
{
if ( root == nullptr )
{
return nullptr ;
}
root-> left = pruneTree ( root-> left) ;
root-> right = pruneTree ( root-> right) ;
if ( root-> left == nullptr && root-> right == nullptr && root-> val == 0 )
{
delete root;
root = nullptr ;
}
return root;
}
} ;
3、解析
四、验证二叉搜索树
1、题目描述
leetcode链接
2、代码
class Solution
{
public :
long prev = LONG_MIN;
bool isValidBST ( TreeNode* root)
{
if ( root == nullptr )
{
return true ;
}
bool Lefttree = isValidBST ( root-> left) ;
if ( Lefttree == false )
return false ;
bool cur = false ;
if ( root-> val > prev)
{
cur = true ;
}
if ( cur == false )
{
return false ;
}
prev = root-> val;
bool Righttree = isValidBST ( root-> right) ;
if ( Lefttree == true && Righttree == true && cur == true )
{
return true ;
}
else
{
return false ;
}
}
} ;
3、解析
五、二叉搜索树中第K小的元素
1、题目描述
leetcode链接
2、代码
class Solution
{
public :
int ret = 0 ;
int count = 0 ;
int kthSmallest ( TreeNode* root, int k)
{
count = k;
dfs ( root) ;
return ret;
}
void dfs ( TreeNode* root)
{
if ( root == nullptr || count == 0 )
{
return ;
}
dfs ( root-> left) ;
count-- ;
if ( count == 0 )
{
ret = root-> val;
return ;
}
dfs ( root-> right) ;
}
} ;
3、解析
六、二叉树的所有路径
1、题目描述
leetcode链接
2、代码
class Solution
{
public :
vector< string> ret;
vector< string> binaryTreePaths ( TreeNode* root)
{
string path;
dfs ( root, path) ;
return ret;
}
void dfs ( TreeNode* root, string path)
{
if ( root == nullptr ) return ;
if ( root-> right == nullptr && root-> left == nullptr )
{
path += to_string ( root-> val) ;
ret. push_back ( path) ;
}
path += to_string ( root-> val) ;
path += "->" ;
dfs ( root-> left, path) ;
dfs ( root-> right, path) ;
}
} ;
class Solution
{
public :
vector< string> ret;
vector< string> binaryTreePaths ( TreeNode* root)
{
string path;
dfs ( root, path) ;
return ret;
}
void dfs ( TreeNode* root, string path)
{
if ( root-> left == nullptr && root-> right == nullptr )
{
path += to_string ( root-> val) ;
ret. push_back ( path) ;
return ;
}
path += to_string ( root-> val) ;
path += "->" ;
if ( root-> left) dfs ( root-> left, path) ;
if ( root-> right) dfs ( root-> right, path) ;
}
} ;
3、解析