zoukankan      html  css  js  c++  java
  • Data Structure Binary Tree: Check for Children Sum Property in a Binary Tree

    http://www.geeksforgeeks.org/check-for-children-sum-property-in-a-binary-tree/

     1 #include <iostream>
     2 #include <vector>
     3 #include <algorithm>
     4 #include <queue>
     5 #include <stack>
     6 using namespace std;
     7 
     8 struct node {
     9     int data;
    10     struct node *left, *right;
    11     node() : data(0), left(NULL), right(NULL) { }
    12     node(int d) : data(d), left(NULL), right(NULL) { }
    13 };
    14 
    15 void print(node *node) {
    16     if (!node) return;
    17     print(node->left);
    18     cout << node->data << " ";
    19     print(node->right);
    20 }
    21 
    22 bool isSumproperty(node *root) {
    23     if (!root || !root->left && !root->right) return true;
    24     int sum = 0;
    25     sum += root->left != NULL? root->left->data : 0;
    26     sum += root->right != NULL? root->right->data : 0;
    27     return sum == root->data && isSumproperty(root->left) && isSumproperty(root->right);
    28 }
    29 
    30 
    31 int main() {
    32     struct node* root = new node(10);
    33     root->left = new node(8);
    34     root->right = new node(2);
    35     root->left->left = new node(3);
    36     root->left->right = new node(5);
    37     root->right->right = new node(2);
    38     if (isSumproperty(root)) cout << "yes" << endl;
    39     else cout << "NO" << endl;
    40     return 0;
    41 }
  • 相关阅读:
    导航守卫
    asnyc
    扩展运算符 ···
    模版字符串
    iterator迭代器
    箭头函数
    解构
    变量let const
    vueUI可视化
    python图像特征提取
  • 原文地址:https://www.cnblogs.com/yingzhongwen/p/3627672.html
Copyright © 2011-2022 走看看