编程之美--求二叉树节点最大距离
来源:互联网 发布:macbook 软件下载 编辑:程序博客网 时间:2024/04/30 10:22
#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;
struct Node
{
bool _visited;
Node* left;
Node* right;
int maxLeft;
int maxRight;
Node()
{
_visited = false;
maxLeft = 0;
maxRight = 0;
left = NULL;
right = NULL;
}
};
int maxLen = 0;
void findMaxLength(Node* root) //编程之美上提供的递归算法
{
if(root==NULL) return;
//如果左子树为空,则该节点左边最长距离为0
if(root->left==NULL)
root->maxLeft=0;
//如果右子树为空,则该节点右边最长距离为0
if(root->right==NULL)
root->maxRight=0;
//如果左子树不为空,递归寻找左边最长距离
if(root->left!=NULL)
findMaxLength(root->left);
//如果右子树不为空,递归寻找右边最长距离
if(root->right!=NULL)
findMaxLength(root->right);
//计算左子树最长节点距离
if(root->left!=NULL)
{
int tempMax=0;
if(root->left->maxLeft > root->left->maxRight)
tempMax=root->left->maxLeft;
else tempMax=root->left->maxRight;
root->maxLeft=tempMax+1;
}
//计算油子树最长节点距离
if(root->right!=NULL)
{
int tempMax=0;
if(root->right->maxLeft > root->right->maxRight)
tempMax=root->right->maxLeft;
else tempMax=root->right->maxRight;
root->maxRight=tempMax+1;
}
//更新最长距离
if(root->maxLeft+root->maxRight > maxLen)
maxLen=root->maxLeft+root->maxRight;
}
stack<Node*> nodeStack;
void findMaxLen( Node* root ) //非递归算法
{
Node* node;
if ( root == NULL )
{
return ;
}
nodeStack.push( root );
while( !nodeStack.empty())
{
node = nodeStack.top();
if ( node->left == NULL && node->right == NULL )
{
nodeStack.pop();
node->_visited = true;
continue;
}
if ( node->left )
{
if ( !node->left->_visited )
{
nodeStack.push( node->left ) ;
}
else
{
node->maxLeft = max( node->left->maxLeft,node->left->maxRight ) + 1;
}
}
if ( ( !node->left || node->left->_visited ) && node->right )
{
if ( !node->right->_visited )
{
nodeStack.push( node->right ) ;
}
else
{
node->maxRight = max( node->right->maxLeft,node->right->maxRight ) + 1;
}
}
if (( !node->left || node->left->_visited ) && ( !node->right || node->right->_visited ))
{
maxLen = max( maxLen, node->maxLeft + node->maxRight );
node->_visited = true;
nodeStack.pop();
}
}
}
int main()
{
Node *tmp ;
Node* root = new Node();
tmp = new Node();
root->left = tmp ;
tmp = new Node();
root->right = tmp;
tmp = new Node();
root->right->right = tmp;
tmp = new Node();
root->right->right->right = tmp;
tmp = new Node();
root->right->right->right->left = tmp;
findMaxLength(root);
//findMaxLen( root );
cout << maxLen << endl;
system("pause");
return 0;
}
- 编程之美--求二叉树节点最大距离
- 编程之美 - 求二叉树节点的最大距离
- 编程之美。求二叉树节点最大距离
- 编程之美 3.8 求二叉树中节点的最大距离 3.9 重建二叉树
- 读书笔记之编程之美 - 3.8 求二叉树中节点的最大距离
- 编程之美之求二叉树中节点的最大距离
- 编程之美读书笔记_3.8 求二叉树中节点的最大距离
- [编程之美:3.8]求二叉树中节点的最大距离
- 求二叉树中节点的最大距离,编程之美3.8
- 《编程之美: 求二叉树中节点的最大距离》的另一个解法
- 编程之美3.8 求二叉树中节点的最大距离
- 《编程之美: 求二叉树中节点的最大距离》的另一个解法
- 编程之美读书笔记_3.8 求二叉树中节点的最大距离
- 编程之美笔记 3.8 求二叉树中节点的最大距离
- 《编程之美: 求二叉树中节点的最大距离》的另一个解法
- 《编程之美: 求二叉树中节点的最大距离》的另一个解法(转)
- 《编程之美: 求二叉树中节点的最大距离》的另一个解法
- [编程之美]求二叉树中节点的最大距离
- VIM基本使用方法
- 动态规划算法简介
- ajax
- java高新技术(一)
- C#中随机验证码生成代码
- 编程之美--求二叉树节点最大距离
- 设计模式-----基于反射工厂模式
- C语言遍历目录
- 求二叉树节点最大距离的非递归算法
- 在经营景城网过程中,发现了很多自身存在的问题,五十四句关于人性的些许总结,惊醒自己,也警示别人。
- 关于 QCC
- Maven中的几个重要概念(三):POM
- 基于VFW的MFC编程实现简易摄像头抓拍
- GDB调试总结