1. 程式人生 > >[leetcode]Same Tree(判斷兩個二叉樹是否相等 C語言實現)

[leetcode]Same Tree(判斷兩個二叉樹是否相等 C語言實現)

Same Tree
Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
題意:比較兩個二叉樹是否相等,對相等的判定為–結構一樣並且節點值也一樣。
解題思路:採用前序遍歷方法遍歷二叉樹,並把遍歷的節點值儲存在陣列中,並且也把每個節點的空子樹也儲存下來(用0表示某個節點的空子樹)。
難點:對二叉樹的遍歷,結構相等則需要把每個節點的左右子樹(包括空子樹也表示在陣列中,類似於一顆完全二叉樹)。
C語言實現程式碼如下:

/**解題思路:採用遞迴遍歷二叉樹,並把各個節點值儲存在陣列中(把每個節點對應的空子樹節點值用0來儲存,類似於儲存一顆完全二叉樹),
 * 通過比較兩個陣列對應值的帶下來判斷兩課二叉樹是否相等
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
void PreOrder(struct TreeNode *root, int *arr, int *i){
    (*i)++;
    if
(root == NULL){ *(arr + *i) = 0;//用0儲存每個節點的空子樹值 return; } *(arr + *i) = root->val; PreOrder(root->left, arr, i); PreOrder(root->right, arr, i); } bool isSameTree(struct TreeNode *p, struct TreeNode *q) { int i,j; int arr1[1000],arr2[1000];//初始化一個數組用於儲存節點值 memset(arr1, 0
, 1000*sizeof(int)); memset(arr2, 0, 1000*sizeof(int)); if(p == NULL && q == NULL){ return 1; } if(p == NULL || q == NULL){ return 0; } j = i = 0; PreOrder(p, arr1, &i); PreOrder(q, arr2, &j); for(i = 0; i < 1000; i++){ if(arr1[i] != arr2[i]){ return 0; } } return 1; }

相關推薦

[leetcode]Same Tree(判斷是否相等 C語言實現)

Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if th

leetcode 100 Same Tree 判斷是否相等

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; *

LeetCode--Same Tree(判斷是否相同)Python

題目: 給定兩個二叉樹,判斷這兩個二叉樹是否完全相同。 解題思路: 直接通過中序遍歷、前序遍歷或者後續遍歷遍歷這兩棵二叉樹,得到兩個list結果,判斷這兩個list是否相同,相同返回True,否則返回False。需要注意考慮葉子節點的情況。 程式碼(Python): #

Leetcode#100. Same Tree(判斷相同)

題目 Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are

007-100-判斷是否相等 Same Tree

Question Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same

判斷是否相等(函式)

bool pd(st *p, st *q) {     if(p==NULL && q==NULL) return true;     else if(p==NULL || q==NU

LeetCode刷題之三:判斷是否相同

題目為: Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are str

每天一道LeetCode-----判斷是否相同

Same Tree 判斷兩個二叉樹是否是相同的,相同的依據是 二叉樹結構相同 二叉樹對應節點值相同 遞迴即可,先判斷當前節點是否相同,然後比較對應的兩個子樹 程式碼如下 /**

面試題(一)---判斷結構是否相同

一、首先這個問題是判斷二叉樹的結構是否相同,所以這就和二叉樹的資料的值無關。只需要判斷結構;判斷兩個二叉樹的結構是否相同很簡單。 採用遞迴的思想: (1)如果兩棵二叉樹都為空,返回真 (2)

用Java程式碼寫一個判斷是否相同

判斷兩個二叉樹是否相同,我覺得應該從三個方面來判斷: 1、若兩個二叉樹都是空樹,則返回true,可認為兩個二叉樹相同; 2、若兩個二叉樹一個為空,一個不為空樹,則兩個二叉樹不相同,返回false; 3、若兩個二叉樹都不為空樹,則判斷兩個節點所指的值是否相同,若相同,則用遞迴

判斷是否相同(c++遞迴實現

判斷兩棵二叉樹是否為同一棵樹,需要比較兩個方面: 一:結構是否相同; 二:每個節點上的元素是否相同; 當二者都滿足的時候才可判定二者為同一棵二叉樹。 結構相同包括節點的個數以及每個節點上的子樹相同。 下面是程式碼實現。 #include<i

資料結構(C語言實現):判斷是否相等,bug求解

判斷兩棵二叉樹是否相等。 遇到了bug,求大神幫忙!!! C語言原始碼: #include <stdio.h> #include <stdlib.h> #include <malloc.h> #define OK 1 #define

判斷是否相同之java實現

package com.cb.java.algorithms.jianzhioffer.tree; /** * 判斷兩個二叉樹是否相同 * * @author 36184 * */ pub

java 比較是否相等

題目: Given two binary trees, write a function to check if they are equal or not. Two binary trees

——判斷是否相等(先序和中序遍歷序列建立

需求: 利用先序遍歷序列和中序遍歷序列來建立兩棵二叉樹,並判斷是否相等 需要先將建立二叉樹 建立的方法是將該二叉樹的先序的序列和中序的序列分別儲存到Pre陣列和Mid陣列中,它們的儲存順序如下: 判斷兩棵樹是否相等 採用遞迴的方法,用先序,中序

資料結構之 AVL(平衡)(C語言實現

AVL樹(平衡二叉樹) 1. AVL樹定義和性質 AVL(Adelson-Velskii和Landis發明者的首字母)樹時帶有平衡條件的二叉查詢樹。 二叉查詢樹的效能分析: 在一顆左右子樹高度平衡情況下,最優的時間複雜度為O(log2n),這與這半

的操作--C語言實現

div break 叠代 二叉樹 node 若是 postorder 元素 初始化 樹是一種比較復雜的數據結構,它的操作也比較多。常用的有二叉樹的創建,遍歷,線索化,線索化二叉樹的遍歷,這些操作又可以分為前序,中序和後序。其中,二叉樹的操作有遞歸與叠代兩種方式,鑒於我個人的

leetcode算法題2: 合並。遞歸,如何切入並保持清醒?

leetcode算法題2: 合並兩個二叉樹。遞歸 如何切入並保持清醒? /* Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees

LeetCode刷題Easy篇是否相同

題目 Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurall

LeetCode 100 給定,編寫一個函式來檢驗它們是否相同。 如果在結構上相同,並且節點具有相同的值,則認為它們是相同的。

給定兩個二叉樹,編寫一個函式來檢驗它們是否相同。 如果兩個樹在結構上相同,並且節點具有相同的值,則認為它們是相同的。 /** * Definition for a binary tree node. * struct TreeNode { *