zl程序教程

您现在的位置是:首页 >  后端

当前栏目

算法-平衡二叉树详解编程语言

2023-06-13 09:20:44 时间
 首先要理解平衡二叉树的定义  同时有两个返回值的处理方法,这里是:是否为平衡二叉树、树的深度。记录树的深度可以避免在每个结点都统计树的深度,减小复杂度。 #include  iostream #include  vector #include  algorithm using namespace std; struct TreeNode{  int val;  TreeNode *left;  TreeNode *right;  TreeNode(int x) : val(x), left(NULL), right(NULL) {} class Solution{ public:  bool IsBalanced_Solution(TreeNode* pRoot){  int depth;  return IsBalanced_Solution(pRoot, depth);  }  bool IsBalanced_Solution(TreeNode* pRoot, int  depth){  if(pRoot == NULL){  depth = 0;  return true;  }  int depthLeft;  int depthRight;  bool ansLeft = IsBalanced_Solution(pRoot- left, depthLeft);  if(ansLeft == false)  return false;  bool ansRight = IsBalanced_Solution(pRoot- right, depthRight);  if(ansRight == false)  return false;  depth = max(depthLeft, depthRight) + 1;  return abs(depthLeft-depthRight)  } int main()  return 0; }

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/15268.html

cgo