示例#1
0
 void find_left_most(TreeNode *root, vector<int> &path)
 {
     if(!root || (root->left == NULL && root->right == NULL))
         return;
     path.push_back(root->val);
     if(root->left)
         find_left_most(root->left, path);
     else if(root->right)
         find_left_most(root->right, path);
 }
示例#2
0
bool BinaryTree<T>::isOrdered(const Node * sub_root) const
{
	// base case:
	if( sub_root == NULL )
	{
		return true;
	}
	// recursive case:
	else
	{
		if( sub_root->left != NULL )
		{
			Node *temp = find_right_most(sub_root);
			if( temp->elem > sub_root->elem )
			{
				return false;
			}
		}
		if( sub_root->right != NULL )
		{
			Node *temp = find_left_most(sub_root);
			if( temp->elem < sub_root->elem )
			{
				return false;
			}
		}
		return isOrdered(sub_root->left)&&isOrdered(sub_root->right);
	}
}
示例#3
0
 vector<int> boundaryOfBinaryTree(TreeNode * root) {
     // write your code here
     
     if(!root)
         return {};
     else if(root->left == NULL && root->right == NULL)
         return {root->val};
         
     vector<int> ans;
     ans.push_back(root->val);
     TreeNode* node;
     
     // Find left boundary
     node = root->left;
     vector<int> left_boundary;
     find_left_most(node, left_boundary);
     
     // Find all leaves
     vector<int> leaves;
     find_leaves(root, leaves);
     
     // Find right boundary, reverse it
     node = root->right;
     vector<int> right_boundary;
     find_right_most(node, right_boundary);
     reverse(right_boundary.begin(), right_boundary.end());
     for(auto x: left_boundary)
         ans.push_back(x);
     for(auto x: leaves)
         ans.push_back(x);
     for(auto x: right_boundary)
         ans.push_back(x);
     return ans;
 }