same tree & depth in the tree

本文探讨了二叉树的相关算法,包括判断两棵二叉树是否相同及计算二叉树的最大深度,并实现了整数反转算法,讨论了其在特定情况下的表现与处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.same tree

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSameTree(TreeNode *p, TreeNode *q) {
        if(p == NULL && q == NULL)
            return true;
        else if(p == NULL || q == NULL)
            return false;
        if(p->val != q->val)
            return false;
        else
            return (isSameTree(p->left,q->left))&&(isSameTree(p->right,q->right));
    }
};

2. depth in the tree

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode *root) {
        
        if(root == NULL)
            return 0;
        
        return 1 + max(maxDepth(root->left),maxDepth(root->right));
    }
};

4. reverse integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).

正负不用考虑,因为本来就不是string,把数字(二进制)方向就好,不用考虑符号,

其次,给的答案是对于尾部是0,不予表示的,

越界也就越界了没有处理


class Solution {
public:
    int reverse(int x) {
        int r = 0;
        while(x != 0)
        {
            r = r * 10 + x % 10;
            x = x / 10;
        }
        return r;
    }
};


这个溢出处理等于没有什么特别的处理啊。

int reverse(int x)
{
	int r = 0;
	const int max = 0x7fffffff;  //int最大值  
    const int min = 0x80000000;  //int最小值  
	while(x != 0)
	{
		r = r * 10 + x % 10;
		if (r > max || r < min)   //溢出处理  
        {  
			r = r > 0 ? max : min;    
            return r; 
		}  
		x =x /10;
	}
	return r;
}



将一系列数字顺序一个个插入一棵初始为空的AVL树。下面哪个系列的第一次旋转是&ldquo;右-左&rdquo;双旋? A. 1,2,3,4,5,6 B. 6,5,4,3,2,1 C. 4,2,5,6,3,1 D. 3,1,4,6,5,2 分数 2 作者 考研真题 单位 浙江大学 Delete a node v from an AVL tree T 1 ​ , we can obtain another AVL tree T 2 ​ . Then insert v into T 2 ​ , we can obtain another AVL tree T 3 ​ . Which one(s) of the following statements about T 1 ​ and T 3 ​ is(are) true? I、If v is a leaf node in T 1 ​ , then T 1 ​ and T 3 ​ might be different. II、If v is not a leaf node in T 1 ​ , then T 1 ​ and T 3 ​ must be different. III、If v is not a leaf node in T 1 ​ , then T 1 ​ and T 3 ​ must be the same. A. I only B. II only C. I and II only D. I and III only 分数 2 作者 何钦铭 单位 浙江大学 Insert 28, 23, 54, 61, 98, 37 into an initially empty AVL tree first. Then immediately insert one of the following keys. Which one will cause an RL rotation? A. 10 B. 30 C. 60 D. 70 分数 2 作者 何钦铭 单位 浙江大学 Insert 28, 23, 54, 61, 98, 37 into an initially empty AVL tree first. Then immediately insert one of the following keys. Which one will cause an RL rotation? A. 10 B. 50 C. 80 D. 100 分数 3 作者 陈越 单位 浙江大学 If there are 14 nodes in an AVL tree, then the maximum depth of the tree is ____. The depth of an empty tree is defined to be 0. A. 3 B. 4 C. 5 D. 6 分数 3 作者 陈越 单位 浙江大学 If there are 28 nodes in an AVL tree, then the maximum depth of the tree is ____. The depth of an empty tree is defined to be -1. A. 3 B. 4 C. 5 D. 6
最新发布
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值