"class Node
{
int val;
Node left, right;
Node(int v)
{
val = v;
left = right = null;
}
}
class BinaryTree
{
Node root1, root2;
boolean identicalTrees(Node a, Node b)
{
if (a == null && b == null)
return true;
if (a != null && b != null)
return (a.val == b.val
&& identicalTrees(a.left, b.left)
&& identicalTrees(a.right, b.right));
"
Tushar A. - "class Node
{
int val;
Node left, right;
Node(int v)
{
val = v;
left = right = null;
}
}
class BinaryTree
{
Node root1, root2;
boolean identicalTrees(Node a, Node b)
{
if (a == null && b == null)
return true;
if (a != null && b != null)
return (a.val == b.val
&& identicalTrees(a.left, b.left)
&& identicalTrees(a.right, b.right));
"See full answer
"let str = 'this is a test of programs';
let obj={};
for (let s of str )
obj[s]?(obj[s]=obj[s]+1):(obj[s]=1)
console.log(JSON.stringify(obj))"
Anonymous Emu - "let str = 'this is a test of programs';
let obj={};
for (let s of str )
obj[s]?(obj[s]=obj[s]+1):(obj[s]=1)
console.log(JSON.stringify(obj))"See full answer
Software Engineer
Data Structures & Algorithms
+2 more
🧠Want an expert answer to a question? Saving questions lets us know what content to make next.
"#include
#include
#include
using namespace std;
void printComs(int prev, int start, int end, int target)
{
if (start >= end) return;
while (start target)
{
end--;
}
else
{
st"
Iris F. - "#include
#include
#include
using namespace std;
void printComs(int prev, int start, int end, int target)
{
if (start >= end) return;
while (start target)
{
end--;
}
else
{
st"See full answer
"function constructTree(n, matrix) {
let parent = [];
let child = [];
let root = null;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (matrixi === 1) {
parent.push(i);
child.push(j);
}
}
}
for (let i = 0; i < n; i++) {
if (parent.indexOf(i) === -1) {
root = i;
}
}
let node = new Node(root);
for (let i = 0; i < n; i++) {
if (i !== root) {
constructTreeUtil(node, parent[i], child[i]);
}
}
return node;
}"
Ugo C. - "function constructTree(n, matrix) {
let parent = [];
let child = [];
let root = null;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (matrixi === 1) {
parent.push(i);
child.push(j);
}
}
}
for (let i = 0; i < n; i++) {
if (parent.indexOf(i) === -1) {
root = i;
}
}
let node = new Node(root);
for (let i = 0; i < n; i++) {
if (i !== root) {
constructTreeUtil(node, parent[i], child[i]);
}
}
return node;
}"See full answer
"We can use a pool of memory where each index we can store the parent of current index then we can use same lca approach for array instead of pointers."
Sourav K. - "We can use a pool of memory where each index we can store the parent of current index then we can use same lca approach for array instead of pointers."See full answer
"The height of a binary tree is the maximum number of edges from the root node to any leaf node. To calculate the height of a binary tree, we can use a recursive approach. The basic idea is to compare the heights of the left and right subtrees of the root node, and return the maximum of them plus one."
Prashant Y. - "The height of a binary tree is the maximum number of edges from the root node to any leaf node. To calculate the height of a binary tree, we can use a recursive approach. The basic idea is to compare the heights of the left and right subtrees of the root node, and return the maximum of them plus one."See full answer
"String commonStr(String str1, String str2) {
int len1 = str1.length();
int len2 = str2.length();
if (len1 == 0 || len2 == 0) return "";
// let dpx reprsent the longest common str of 0...x
int dp = new int len1 + 1;
int maxLen = 0;
int endIndex = 0;
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
if (str1.charAt(i-1) == str2.charAt(j-1)) {
dpi = dpi-1 + 1;
"
Emma X. - "String commonStr(String str1, String str2) {
int len1 = str1.length();
int len2 = str2.length();
if (len1 == 0 || len2 == 0) return "";
// let dpx reprsent the longest common str of 0...x
int dp = new int len1 + 1;
int maxLen = 0;
int endIndex = 0;
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
if (str1.charAt(i-1) == str2.charAt(j-1)) {
dpi = dpi-1 + 1;
"See full answer