把二叉搜索树变成一个二叉树,使得原来每个节点的值 变成 (原来的值+所有大于它的值的和)

Given a Binary Search Tree (BST), convert it to a Binary Tree such that every key of the original BST is changed to key plus sum of all greater keys in BST.

Examples:
Input: Root of following BST
                5
              /   \
           2     13
Output: The given BST is converted to following Binary Tree
              18
              /   \
          20     13

使用递归的方法,递归函数定义为 convert(Node* root, int& sum),root是子树的根节点,sum是该子树所有节点的和。

方法一代码如下:

#include<stdio.h>
#include<stdlib.h>
 
struct Node{  
        int value;  Node* lchild;  Node* rchild;  
        Node(int val, Node* lch, Node* rch):value(val), lchild(lch), rchild(rch) {}       
};

void convert(Node* root, int& sum)
{
	if(root==NULL)
		return;
	convert(root->rchild, sum);
	root->value = sum + root->value;
	sum = root->value;
	convert(root->lchild, sum);
}

void print(Node* root)
{
	if(root){
		printf("%d|",root->value);
		print(root->lchild);  print(root->rchild);
	}
}

int main()
{
    Node* n1 = new Node(1,NULL,NULL);  
    Node* n3 = new Node(3,NULL,NULL);  
    Node* n2 = new Node(2,n1,n3);  
    Node* n5 = new Node(5,NULL,NULL);  
    Node* n4 = new Node(4,n2,n5); 
  /* Constructed binary tree is 
            4 
          /   \ 
        2      5 
      /  \ 
    1     3 
  */  
	int sum = 0;   convert(n4, sum);
	// convert(n4, 15) is wrong, since a temporary object cannot be assigned to a non-const reference
	print(n4);
	getchar();
	return 0;
}

方法二也采用递归:

#include<stdio.h>
#include<stdlib.h>
 
struct Node{  
        int value;  Node* lchild;  Node* rchild;  
        Node(int val, Node* lch, Node* rch):value(val), lchild(lch), rchild(rch) {}       
};

void convert(Node* root, int& sum)
{
	if(root==NULL)
		return;
	if(root->lchild)
		convert(root->lchild, sum);
	int temp = root->value;
	root->value = sum;
	sum = sum - temp;
	convert(root->rchild, sum);
}

void print(Node* root)
{
	if(root){
		printf("%d|",root->value);
		print(root->lchild);  print(root->rchild);
	}
}

int main()
{
    Node* n1 = new Node(1,NULL,NULL);  
    Node* n3 = new Node(3,NULL,NULL);  
    Node* n2 = new Node(2,n1,n3);  
    Node* n5 = new Node(5,NULL,NULL);  
    Node* n4 = new Node(4,n2,n5); 
  /* Constructed binary tree is 
            4 
          /   \ 
        2      5 
      /  \ 
    1     3 
  */  
	int sum = 15;   convert(n4, sum);
	// convert(n4, 15) is wrong, since a temporary object cannot be assigned to a non-const reference
	print(n4);
	getchar();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉搜索树(BST)的定义是:对于树中的每个节点,其左子树中的所有节点都小于该节点,其右子树中的所有节点大于节点。 对于二叉树的广义表表示,我们可以按照以下步骤来判断是否为BST: 1. 将广义表表示转换为二叉树的数据结构,可以使用递归方法实现。具体方法是:从广义表中读取当前节点,然后读取下一个节点,如果下一个节点是“(”,说明当前节点有左子树,继续递归读取左子树;如果下一个节点是数字,说明当前节点没有左子树,将读取的数字作为当前节点的左子节点;如果下一个节点是“)”,说明当前节点没有左子树,左子节点为空。同理可以读取右子树。 2. 对于每个节点,判断其是否满足BST的定义,即其左子树的所有节点都小于该节点,其右子树的所有节点大于节点。可以使用递归方法实现。具体方法是:对于每个节点,检查其左子树是否满足BST的定义,检查其右子树是否满足BST的定义,同时检查其左子树中的所有节点是否都小于该节点,其右子树中的所有节点是否都大于节点。 下面是C语言实现的代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> // 二叉树节点结构体 struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; // 读取一个节点 struct TreeNode* readNode(char** s) { int num = 0; bool negative = false; if (**s == '-') { negative = true; (*s)++; } while (**s >= '0' && **s <= '9') { num = num * 10 + **s - '0'; (*s)++; } if (negative) { num = -num; } struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode)); node->val = num; node->left = NULL; node->right = NULL; return node; } // 读取一个子树 struct TreeNode* readSubtree(char** s) { (*s)++; // 跳过 '(' if (**s == ')') { (*s)++; // 跳过 ')' return NULL; } struct TreeNode* root = readNode(s); root->left = readSubtree(s); root->right = readSubtree(s); (*s)++; // 跳过 ')' return root; } // 广义表表示转换为二叉树 struct TreeNode* treeFromStr(char* s) { if (*s == '(') { return readSubtree(&s); } else { return NULL; } } // 判断一棵树是否为BST bool isBST(struct TreeNode* root, int min, int max) { if (root == NULL) { return true; } if (root->val <= min || root->val >= max) { return false; } return isBST(root->left, min, root->val) && isBST(root->right, root->val, max); } int main() { char* s = "(5(3(2)(4))(8(6)(10)))"; // BST struct TreeNode* root = treeFromStr(s); bool result = isBST(root, INT_MIN, INT_MAX); printf("%s\n", result ? "true" : "false"); s = "(5(3(2)(4))(8(10)(6)))"; // not BST root = treeFromStr(s); result = isBST(root, INT_MIN, INT_MAX); printf("%s\n", result ? "true" : "false"); return 0; } ``` 输出结果为: ``` true false ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值